<template>
  <div class="main">
    <div class="form">
      <div class="mask">
        <h3 class="title utils-text-mask">数字孪生管理平台</h3>
      </div>
      <el-form
          :model="ruleForm"
          :rules="rules"
          ref="loginRules"
          class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
              v-model="ruleForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" class="deep_element_btn">
          <el-input
              type="password"
              v-model="ruleForm.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="btn">
        <el-button type="primary" @click="submitForm()">登录</el-button>
        <el-button type="success" @click="resit">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {inject, onMounted, reactive, toRefs, ref, unref} from "vue";
import {ElMessage} from 'element-plus';
import {useRouter} from 'vue-router';


export default {
  name: "login",
  components: {},
  setup() {
    let data = reactive({
      ruleForm: {
        username: "",
        password: "",
        action: "password"
      },
    })
    const rules = {
      username: [
        {required: true, message: "请输入用户名", trigger: "blur"},
      ],
      password: [
        {required: true, message: "请输入密码", trigger: "blur"},
        {
          min: 6,
          max: 20,
          message: "长度在 6 到 20 个字符",
          trigger: "blur",
        },
      ],
    }
    let loginRules = ref(null)
    const $http = inject("$http")
    const router = useRouter()

    onMounted(() => {
      document.onkeydown = function (e) {
        if (e.code == "Enter") {
          submitForm()
        }
      }
    })
    const submitForm = () => {
      loginRules.value.validate(async (valid) => {
        if (!valid)
          return;
        const res = await $http.user.login(data.ruleForm)
        console.log(res)
        if (res.code === 1) {
          ElMessage.success(res.msg);
          window.sessionStorage.setItem('token', res.result.token);
          router.push({
            path: '/home'
          })
        } else {
          ElMessage.error(res.msg);
        }
      });
    }

    const resit = () => {
      this.ruleForm.username = ""
      this.ruleForm.password = ""
    }
    return {
      ...toRefs(data),
      rules,
      loginRules,
      submitForm,
      resit
    }
  },
  computed: {},
  methods: {},
  mounted() {
  },
};
</script>

<style scoped lang="less">
@border_color: #eceaea;
.main {
  width: 100%;
  height: 100%;
  background-image: url("../../assets/bg.jpg");

  background-size: 100% 100%;
  box-sizing: border-box;
  display: flex;
  //position: relative;

  .title {
    text-align: center;
    color: #fff;
    line-height: 40px;
    height: 40px;
    font-size: 28px;
    letter-spacing: 2px;
    margin-bottom: 20px;
  }

  .form {
    margin: auto;
    width: 500px;
    height: 300px;
    padding: 20px 20px 20px 10px;
    //position: absolute;
    //left: 35%;
    //top: 20%;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-radius: 10px;
    box-shadow: 0px 0px 10px #ccc;
    animation: loginBg 4s infinite ease-in;
  }

  .el-form {
    border-top: 1px solid @border_color;
    border-bottom: 1px solid @border_color;
    padding: 20px 0;

    /deep/ .el-form-item:first-child {
      margin-bottom: 30px;
    }

    /deep/ .el-form-item:last-child {
      margin-bottom: 0px;
    }
  }

  .btn {
    position: relative;
    height: 60px;

    .el-button {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
    }

    .el-button:first-child {
      left: 20%;
    }

    .el-button:last-child {
      right: 20%;
    }
  }
}

/deep/ .el-input__inner {
  background-color: rgba(43, 199, 246, 0.36) !important;
  color: white !important;

  &::-webkit-input-placeholder {
    color: white !important;
  }
}

@keyframes loginBg {
  0% {
    box-shadow: 0px 0px 5px #ccc;
  }
  50% {
    box-shadow: 0px 0px 25px #ccc;
  }
  100% {
    box-shadow: 0px 0px 5px #ccc;
  }
}


</style>
